package com.bit.springbook.service;

import com.bit.springbook.enums.BookStatusEnum;
import com.bit.springbook.mapper.BookInfoMapper;
import com.bit.springbook.model.BookInfo;
import com.bit.springbook.model.PageResult;
import com.bit.springbook.model.RequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    // 添加图书
    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.insertBookInfo(bookInfo);
    }

    // 分页查询
    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        int total = bookInfoMapper.getTotalCount();
        List<BookInfo> list = bookInfoMapper.getListByPage
                (requestPage.getOffset(), requestPage.getPageSize());
        list.forEach((e) -> {
            e.setStatusCN(BookStatusEnum.getStatusByCode(e.getStatus()).getName());
        });
        return new PageResult<>(list, total);
    }

    public BookInfo getBookInfo(Integer id) {
        return bookInfoMapper.getBookInfo(id);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Boolean deleteBook(Integer id) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(id);
        bookInfo.setStatus(BookStatusEnum.INVALID.getCode());
        Integer delete = bookInfoMapper.updateBook(bookInfo);
        return delete > 0;
    }

    public Boolean deleteBatch(List<Integer> ids) {
        Integer rows = bookInfoMapper.deleteBatch(ids);
        return rows >= 0;
    }
}
